﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using DataAccess;

public partial class Manager_ChangePasswordManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Message"] != null)
        {
            lblmes.Text = Session["Message"].ToString();
            Session.Remove("Message");
        }
        else
            lblmes.Text = "";
    }
    protected void btnChange_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //get field
            string PasswordCurrent = txtPasswordCurrent.Text;
            string Password = txtPassword.Text;
            string ConfirmPassword = txtConfirmPassword.Text;
            string md5Pass = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordCurrent, "MD5").ToLower();
            if (Session["AdminPassword"].ToString() == md5Pass)
            {
                if (Password == ConfirmPassword)
                {
                    string md5Passnew = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5").ToLower();
                    DbCommand cmd = GenericData.CreateCommand();
                    cmd.CommandText = "UPDATE Manager SET Password = '" + md5Passnew + "' WHERE ID = " + Session["ManagerID"].ToString();
                    if (GenericData.ExecuteNoneQuery(cmd) > 0)
                        Session.Add("Message", "Change password successfully");
                    else
                        Session.Add("Message", "<font color='red'>Error : Change password unsuccess</font>");
                }
                else
                {
                    Session.Add("Message", "<font color='red'>Error : Password and Confirm Password not match</font>");
                }
            }
            else
            {
                Session.Add("Message", "<font color='red'>Error : Password current wrong</font>");
            }
        }
    }
}